package com.bw.controller.invoice;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bw.controller.customer.PiCustomerController;
import com.bw.entity.customer.PiCustomer;
import com.bw.entity.invoice.PiInvoice;
import com.bw.entity.invoice.PiInvoiceLog;
import com.bw.entity.linkMan.Linkman;
import com.bw.entity.orders.PiOrder;
import com.bw.entity.orders.PiOrderLog;
import com.bw.entity.user.TbUser;
import com.bw.resp.ResponseResult;
import com.bw.service.invoice.IPiInvoiceLogService;
import com.bw.service.invoice.IPiInvoiceService;
import com.bw.service.orders.IPiOrderService;
import com.bw.service.user.ITbUserService;
import com.bw.utils.idwork.IdWorker;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yinxurui
 * @since 2020-06-16
 */
@RestController
@RequestMapping("invoice")
public class PiInvoiceController {

    @Autowired
    private IPiInvoiceService iPiInvoiceService;

    @Autowired
    private IPiOrderService orderService;

    @Autowired
    private ITbUserService userService;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private IPiInvoiceLogService iPiInvoiceLogService;
    private final Logger logger= LoggerFactory.getLogger(PiInvoiceController.class);


    /**
     * @Description:修改状态
     * @Author: 周明丽
     * @Date: 2020/7/1 19:11
     * @param id:
     * @return: com.bw.resp.ResponseResult
     **/
    @GetMapping("find/{id}")
    public  ResponseResult findInvoice(@PathVariable("id") long id){
        ResponseResult result=ResponseResult.SUCCESS();
        logger.info("查询开始");
        try {
            PiInvoice piInvoice=this.iPiInvoiceService.getById(id);
            result.setResult(piInvoice);
        }catch (Exception e){
            logger.error("修改失败"+e.toString());
            result.setMessage("修改失败"+e.toString());
        }
        return  result;
    }
     /**
      * @Description:提交发票
      * @Author: 尹绪瑞 
      * @Date: 2020/7/1 20:39
      * @param invoiceLog: 
      * @return: com.bw.resp.ResponseResult
      **/ 
    @PostMapping("find1")
    public  ResponseResult find(@RequestBody PiInvoiceLog invoiceLog){
        ResponseResult result=ResponseResult.SUCCESS();
        logger.info("开始操作");
        try {
            PiInvoice piInvoice=this.iPiInvoiceService.getById(invoiceLog.getInvoiceId());
            piInvoice.setState(1);//提交
            invoiceLog.setRemarks("提交");
            this.iPiInvoiceService.saveOrUpdate(piInvoice);
            this.iPiInvoiceLogService.saveOrUpdate(invoiceLog);
        }catch (Exception e){
            logger.error("修改失败"+e.toString());
            result.setMessage("修改失败"+e.toString());
        }
        return  result;
    }
    @PostMapping("find4")
    public  ResponseResult find4(@RequestBody PiInvoiceLog invoiceLog){
        ResponseResult result=ResponseResult.SUCCESS();
        logger.info("开始操作");
        try {
            PiInvoice piInvoice=this.iPiInvoiceService.getById(invoiceLog.getInvoiceId());
            piInvoice.setState(2);//提交
            invoiceLog.setRemarks("通过");
            this.iPiInvoiceService.saveOrUpdate(piInvoice);
            this.iPiInvoiceLogService.saveOrUpdate(invoiceLog);
        }catch (Exception e){
            logger.error("修改失败"+e.toString());
            result.setMessage("修改失败"+e.toString());
        }
        return  result;
    }
    /**
     * @Description:修改状态
     * @Author: 周明丽
     * @Date: 2020/7/1 19:11
     * @param invoiceLog:
     * @return: com.bw.resp.ResponseResult
     **/
    @PostMapping("find5")
    public  ResponseResult find5(@RequestBody PiInvoiceLog invoiceLog){
        ResponseResult result=ResponseResult.SUCCESS();
        logger.info("开始操作");
        try {
            PiInvoice piInvoice=this.iPiInvoiceService.getById(invoiceLog.getInvoiceId());
            piInvoice.setState(5);
            invoiceLog.setRemarks("签收");
            this.iPiInvoiceService.saveOrUpdate(piInvoice);
            this.iPiInvoiceLogService.saveOrUpdate(invoiceLog);
        }catch (Exception e){
            logger.error("修改失败"+e.toString());
            result.setMessage("修改失败"+e.toString());
        }
        return  result;
    }
    /**
     * @Description:
     * @Author: 尹绪瑞 
     * @Date: 2020/6/29 8:52
     * @param code: 
     * @param logisticsCym:
     * @param invoiceLog:
     * @return: com.bw.resp.ResponseResult
     **/
    
    @PostMapping("find2/{code}/{logisticsCym}")
    public  ResponseResult find2(@PathVariable("code")String code,@PathVariable("logisticsCym")String logisticsCym, @RequestBody PiInvoiceLog invoiceLog){
        ResponseResult result=ResponseResult.SUCCESS();
        logger.info("开始操作");
        try {
            PiInvoice piInvoice=this.iPiInvoiceService.getById(invoiceLog.getInvoiceId());
            piInvoice.setState(4);//提交
            piInvoice.setCode(code);
            piInvoice.setLogisticsCym(logisticsCym);
            invoiceLog.setRemarks("邮寄");
            this.iPiInvoiceService.saveOrUpdate(piInvoice);
            this.iPiInvoiceLogService.saveOrUpdate(invoiceLog);
        }catch (Exception e){
            logger.error("修改失败"+e.toString());
            result.setMessage("修改失败"+e.toString());
        }
        return  result;
    }

    /**
     * @Description:修改状态
     * @Author: 周明丽
     * @Date: 2020/7/1 19:11
     * @param invoiceLog:
     * @return: com.bw.resp.ResponseResult
     **/
    @PostMapping("find3")
    public  ResponseResult find1(@RequestBody PiInvoiceLog invoiceLog){
        ResponseResult result=ResponseResult.SUCCESS();
        logger.info("开始操作");
        try {
            PiInvoice piInvoice=this.iPiInvoiceService.getById(invoiceLog.getInvoiceId());
            piInvoice.setState(3);//提交
            this.iPiInvoiceService.saveOrUpdate(piInvoice);
            this.iPiInvoiceLogService.saveOrUpdate(invoiceLog);
        }catch (Exception e){
            logger.error("修改失败"+e.toString());
            result.setMessage("修改失败"+e.toString());
        }
        return  result;
    }


    /**
     * @Description:分页
     * @Author: 周明丽
     * @Date: 2020/7/1 19:11
     * @param :
     * @return: com.bw.resp.ResponseResult
     **/
    @PostMapping("/list/{pagenum}/{pagesize}/{id}")
    public ResponseResult list(@PathVariable("pagenum")int pagenum, @PathVariable("pagesize")int pagesize,@PathVariable("id") Long id){
        logger.info("开始查询列表");
        ResponseResult result = ResponseResult.SUCCESS();
        PageHelper.startPage(pagenum,pagesize);
        try {
           if (id!=0){
            QueryWrapper wrapper=new QueryWrapper();
            wrapper.eq("customerId",id);
               List list =this.iPiInvoiceService.list(wrapper);
               PageInfo pageInfo = new PageInfo(list);
               result.setResult(pageInfo);
               logger.info("查询成功1");
           }else{
               List list =this.iPiInvoiceService.list();
               PageInfo pageInfo = new PageInfo(list);
               result.setResult(pageInfo);
               logger.info("查询成功");
           }
        }catch (Exception e){
            logger.error("查询错误"+e.toString());
            result.setSuccess(false);
            result.setMessage("查询失败"+e.toString());
        }
        logger.info("查询结束");
        return result;
    }




    /**
     * @Description:发票保存
     * @Author: 周明丽
     * @Date: 2020/6/26 17:55
     * @param invoice:
     * @return: com.bw.resp.ResponseResult
     **/
    @PostMapping("save/{orderid}")
    public ResponseResult save(@PathVariable("orderid")long orderid,@RequestBody PiInvoice invoice) {
        logger.info("保存客户信息开始");
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            PiOrder order = orderService.getById(orderid);
            order.setState(5);
            orderService.saveOrUpdate(order);
            invoice.setCreateUser(invoice.getCustomerId());
            invoice.setState(0);
            invoice.setCode(order.getCode());
            invoice.setCreateDate(new Date());
            this.iPiInvoiceService.saveOrUpdate(invoice);
            result.setMessage("保存成功");
        } catch (Exception e) {
            logger.error("保存失败"+e.toString());
            result.setSuccess(false);
            result.setMessage("保存失败" + e.toString());
        }
        logger.info("保存客户信息结束");
        return result;
    }

    /**
     * @Description:可开票总金额
     * @Author: 周明丽
     * @Date: 2020/6/27 20:23
     * @param customerId:
     * @return: com.bw.resp.ResponseResult
     **/
    @GetMapping("getMoney/{customerId}")
    public ResponseResult getCusOrders(@PathVariable("customerId") long customerId){
        ResponseResult result=ResponseResult.SUCCESS();
        logger.info("查询客户信息开始");
        BigDecimal money1=new BigDecimal(0);

        try {
            QueryWrapper queryWrapper = new QueryWrapper();

            //查询待支付，已支付的所有金额
            queryWrapper.eq("customerId",customerId);
            queryWrapper.eq("state",4);//已支付
            List<PiOrder> piOrders = orderService.list(queryWrapper);
            for(PiOrder piOrder:piOrders){
                if(piOrder.getPriceIt()!=null){
                    money1=money1.add(piOrder.getPriceIt());
                }

            }
            QueryWrapper queryWrapper1 = new QueryWrapper();
            queryWrapper1.eq("customerId",customerId);
            queryWrapper1.eq("state",4);//邮寄
            queryWrapper1.or();
            queryWrapper1.eq("state",5);//签收
            queryWrapper1.or();
            queryWrapper1.eq("state",2);//通过
            List<PiInvoice> piInvoices = iPiInvoiceService.list(queryWrapper1);
            for(PiInvoice piInvoice:piInvoices){
                money1=money1.subtract(piInvoice.getAmount());
            }

            result.setResult(money1);
            result.setMessage("查询成功");
        }catch (Exception e){
            logger.error("查询客户信息异常");
            result=ResponseResult.FAIL();
            // result.setResult("发送失败"+e.toString());
        }
        logger.info("查询客户信息结束");
        return result;
    }


}
